from django.shortcuts import render
from django.http import HttpResponse
import hashlib

# Create your views here.
def toolsView(request):
    return render(request, 'index.html')

def crypt(request):
    print(request.POST)

    if request.POST['crypt'] == 'encrypt':
        orginT = request.POST['originText']
        m = None
        if request.POST['cryptType'] == 'MD5':
            m = hashlib.md5()
        elif request.POST['cryptType'] == 'SHA1':
            m = hashlib.sha1()
        elif request.POST['cryptType'] == 'SHA256':
            m = hashlib.sha256()
        else:
            return render(request, 'index.html', {'encryptedText': 'error', 'originText': request.POST['originText'], 'cryptType':request.POST['cryptType']})
        m.update(orginT.encode("utf-8"))  # 参数必须是byte类型，否则报Unicode-objects must be encoded before hashing错误
        md5value = m.hexdigest()
        print(request.POST['cryptType']+' : '+md5value)
        return render(request, 'index.html', {'encryptedText': md5value, 'originText': request.POST['originText'], 'cryptType':request.POST['cryptType']})
        # return HttpResponse(request.POST)
    else:
        return render(request, 'index.html', {'encryptedText': request.POST['originText'], 'originText': request.POST['originText'], 'cryptType':request.POST['cryptType']})